﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using System.Web.Security;

namespace TireService.Web.View.AdminView
{
    public partial class ImportTireSetdata : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            // Bind gridview
            BindTireSetList(db);

            // Change gridview pagesize based on dropdownlist
            gvTireSet.SettingsPager.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);

            // Dispose connection to database
            db.Dispose();
        }

        /// <summary>
        /// Function that bind cars to gridview
        /// </summary>
        /// <param name="db">Connection to database</param>
        private void BindTireSetList(TireServiceDB db)
        {
            // Bind to gridview - show all cars that has had service
            gvTireSet.DataSource = db.tmpTiresets.Where(p => p.Updated == false).ToList();
            gvTireSet.DataBind();
        }



        protected void btnSendSMS_Click(object sender, EventArgs e)
        {
            EmailNotificationServices emailBLL = new EmailNotificationServices();
            emailBLL.SendSMS();
        }


        protected void btnMassUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                TireServiceDB db = new TireServiceDB();

                List<tmpTireset> tires = db.tmpTiresets.Where(p => p.Updated == false).ToList();

                foreach (tmpTireset tire in tires)
                {
                    // Get tireset
                    List<TireSet> tireset = db.GetTireSets(tire.CarId).Where(p => p.ContainerCargoRoomId != null).ToList();

                    if (tireset != null)
                    {
                        foreach (TireSet t in tireset)
                        {
                            if (t.TireSetStatusId != (int)Shared.TireSetStatus.MountedOnCar)
                            {

                                ContainerCargoRoom room = db.GetContainerCargoRooms(1).SingleOrDefault(p => p.CargoRoomNumber == tire.ContainerCargoRoom);

                                if (room != null)
                                {
                                    // Report tireset on stock
                                    if (t.TireSetStatusId == (int)Shared.TireSetStatus.AwaitsMoveToStock)
                                    {
                                        t.TireSetStatusId = (int)Shared.TireSetStatus.OnStock;
                                    }
                                    
                                    int NewContainerCargoRoomId = room.ContainerCargoRoomId;

                                    // Create tiresetlog for tireset
                                    TireSetLogBLL bll = new TireSetLogBLL();

                                    // Check if selected containercargoroom is already used
                                    TireSet TireSetOccupying = db.TireSets.SingleOrDefault(p => p.ContainerCargoRoomId == NewContainerCargoRoomId);

                                    int containercargoroomid = Convert.ToInt32(t.ContainerCargoRoomId);

                                    if (TireSetOccupying == null)
                                    {
                                        // No tireset is occupying the cargoroom already

                                        // Update tireset
                                        t.ContainerCargoRoomId = NewContainerCargoRoomId;

                                        // Create log
                                        bll.CreateTireSetLog(db, t, DateTime.Now, t.TireSetStatusId, t.TireSetStatusId, new Guid(Membership.GetUser().ProviderUserKey.ToString()), "Hjul flyttet til ny lagerplads", null, null, null, null, NewContainerCargoRoomId);
                                    }
                                    else
                                    {
                                        // Other tireset occupy cargoroom - move around

                                        // Reset occupying tireset cargoroom
                                        TireSetOccupying.ContainerCargoRoomId = null;

                                        // Update tireset
                                        t.ContainerCargoRoomId = NewContainerCargoRoomId;

                                        // Create log
                                        bll.CreateTireSetLog(db, t, DateTime.Now, t.TireSetStatusId, t.TireSetStatusId, new Guid(Membership.GetUser().ProviderUserKey.ToString()), "Hjul flyttet til ny lagerplads", null, null, null, null, NewContainerCargoRoomId);

                                        // Give occupying tireset new cargoroom
                                        TireSetOccupying.ContainerCargoRoomId = containercargoroomid;

                                        // Create log for occupying tireset
                                        bll.CreateTireSetLog(db, TireSetOccupying, DateTime.Now, TireSetOccupying.TireSetStatusId, TireSetOccupying.TireSetStatusId, new Guid(Membership.GetUser().ProviderUserKey.ToString()), "Hjul flyttet til ny lagerplads", null, null, null, null, TireSetOccupying.ContainerCargoRoomId);
                                    }

                                    // Update tireset
                                    t.TireBrandId = tire.TireBrandId;
                                    t.TireBrandNote = tire.TireBrandNote;
                                    t.Width = tire.Width;
                                    t.Height = tire.Height;
                                    t.Size = tire.Size;
                                    t.TireLoadIndexId = tire.TireLoadIndexId;
                                    t.TireSpeedIndexId = tire.TireSpeedIndexId;
                                    t.RimType = tire.RimType;
                                    t.ExtraBolts = tire.ExtraBolts;
                                    t.HubCaps = tire.HubCaps;
                                    t.LockingBolt = tire.LockingBolt;
                                    t.LeftFrontTirePattern = tire.LeftFrontTirePattern;
                                    t.LeftRearTirePattern = tire.LeftRearTirePattern;
                                    t.RightFrontTirePattern = tire.RightFrontTirePattern;
                                    t.RightRearTirePattern = tire.RightRearTirePattern;
                                    t.Note = tire.Note;
                                    //t.ContainerCargoRoomId = room.ContainerCargoRoomId;

                                    tire.Updated = true;

                                    db.SubmitChanges();
                                }
                                

                            }
                        }
                    }
                    
                }

                // Bind gridview
                BindTireSetList(db);

                db.Dispose();

                lMessage.Text = "Opdatering gennemført";
                
            }
            catch (Exception ex)
            {
                string error = ex.Message;

                lMessage.Text = error;
            }

            
        }

        protected void btnCreateUsers_Click(object sender, EventArgs e)
        {
            try
            {
                TireServiceDB db = new TireServiceDB();

                List<Customer> customers = db.Customers.Where(p => p.UserId == null).Take(50).ToList();

                string fejl = "";

                foreach (Customer customer in customers)
                {
                    try
                    {
                        // Create new user
                        MembershipUser newuser = Membership.CreateUser(customer.Email, customer.Phone1.ToString(), customer.Email);
                    
                        if (customer.IsCompany)
                        {
                            // Business customer
                            // Add user to business customer role
                            Roles.AddUserToRole(newuser.UserName, "Erhvervskunde");
                        }
                        else
                        {
                            // Add user to customer role
                            Roles.AddUserToRole(newuser.UserName, "Kunde");
                        }


                        customer.UserId = new Guid(newuser.ProviderUserKey.ToString());

                        db.SubmitChanges();
                    }
                    catch (Exception ex)
                    {
                        fejl += "<br>" + customer.Email;
                    }
                }

                db.Dispose();

                lMessage.Text = "Brugere oprettet" + " - Fejl: " + fejl;
            }
            catch (Exception ex)
            {
                string error = ex.Message;

                lMessage.Text = error;
            }


        }
    }
}